import time
from multiprocessing import Process
from multiprocessing.connection import Client


class GameEngineProcess(Process):
    def __init__(self, address, authkey, game_envoler):
        super().__init__()
        self.daemon = True
        self._address = address
        self._authkey = authkey
        self._conn = None
        # self._cond = cond
        self.observations = ""
        self.actions = ""
        self._step_func = game_envoler  # 此处为调试代码，运行代码为self._step_func = game_envoler.step

    def run(self):
        self._conn = Client(address=self._address, authkey=self._authkey)
        while True:
            rcv_data = self._conn.recv()
            print(f"\t  Engine <- Scheduler: {rcv_data}")
            if rcv_data == "ready":
                print(f"\t  Engine -> Scheduler: 'ok'")
                self._conn.send("ok")
            elif rcv_data == "done":
                break
            else:
                self.actions = rcv_data
                self.observations = self._step_func(400000)  # 模拟game_envolver的step方法，生成态势self.observation
                print(f"\t  Engine -> Scheduler: {self.observations}")
                self._conn.send(self.observations)
                self.observations = ""                
    